
package com.bening.smsapp.parser.thread;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.bening.smsapp.constant.*;
import com.bening.smsapp.parser.thread.worker.AsciiParserWorker;
import com.bening.smsapp.parser.thread.worker.Asn1ParserWorker;

public class WorkerThread extends Thread {

   private static Log log = LogFactory.getLog(WorkerThread.class);

   public WorkerThread() {
      //default constructor
   }
   private boolean stopped = false;
   private boolean running = false;
   private boolean done = true;
   private int type = 0;
   private ThreadPool pool = null;
   private Object syncObject = null;
   String localPath = "";
   String filename = "";
   int sourcePath = 0;

   public void setPool(ThreadPool pool) {
      this.pool = pool;
   }
   public boolean isDone() {
      return done;
   }
   public void setDone(boolean done) {
      this.done = done;
   }
   public boolean isRunning() {
      return running;
   }
   public void setRunning(boolean running) {
      this.running = running;
   }
   public boolean isStopped() {
      return stopped;
   }
   public void setStopped(boolean stopped) {
      this.stopped = stopped;
   }
   
   public synchronized void execute(int type,String localPath, String fileName, int sourcePath) {
      this.done = false;
      this.type = type;
      this.localPath = localPath;
      this.filename = fileName;
      this.sourcePath = sourcePath;
      
      if (!running) { //If this is the first time, then kick off the thread.
         this.setDaemon(true);
         this.start();
      } else { // we already have a thread running so wakeup the waiting thread.
         this.notifyAll();
      }
   }

   /* (non-Javadoc)
    * @see java.lang.Runnable#run()
    */
   public void run() {
      running = true;
      
      while (!stopped) {
         if (done) {
            synchronized (this) {
               try {
                  this.wait();
               } catch (InterruptedException e) {
                  stopped = true;
                  log.error("", e);
               }
            }
         } else { //there is a task....let us execute it.
            try {
               execute();
            } catch (Exception e) {
               log.error("", e);
            } finally {
               if (syncObject != null) {
                  synchronized (syncObject) {
                     syncObject.notify();
                  }
               }
               reset();
               returnToPool();
            }
         }
      }
   }
   
   private void returnToPool() {
      if(pool != null) { 
         try {
            pool.returnObject(this);
         } catch (Exception e1) {
            log.error("Exception :", e1);
         }
         this.pool = null;
      }
   }

   public void reset() {
      this.done = true;
      this.syncObject = null;
      this.type = 0;
   }

   private void execute() {
	   if(this.type==SmsAppConstant.ASCII) {
		   log.info(Thread.currentThread().getName()+"-Got ascii type, creating parse instance");
		   AsciiParserWorker asc = new AsciiParserWorker();
		   asc.parseFileToDatabase(this.localPath, this.filename, this.sourcePath);
	   }else if(this.type==SmsAppConstant.ASN1) {
		   Asn1ParserWorker asn = new Asn1ParserWorker();
		   log.info(Thread.currentThread().getName()+"-Got asn1 type, creating parse instance");
		   asn.parseFileToDatabase(this.localPath, this.filename, this.sourcePath);
	   }
		   
   }

}